﻿@using Think9.Models;
@{ ViewBag.Title = "Add";
    Layout = "~/Areas/Shared/_LayuiForm.cshtml"; }

<form class="layui-form">
    <input style="display:none" id="frm" value="@ViewBag.frm">
    <div class="layui-form-item">
        <label class="layui-form-label">指标分类</label>
        <div class="layui-input-block">
            @Html.DropDownList("IndexSort", (IEnumerable<SelectListItem>)ViewBag.SortList, "==请选择指标分类==", new Dictionary<string, object> { })
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">指标编码</label>
        <div class="layui-inline">
            <input type="text" name="IndexId" id="IndexId" placeholder="指标编码" autocomplete="off" class="layui-input" lay-verify="required|id" maxlength="30">
        </div>
        <div class="layui-inline" style="width:140px">
            <select name="AutoType" id="AutoType" lay-filter="selectfilter">
                <option value="">⇓自动生成编码</option>
                <option value="英文">英文</option>
                <option value="全拼">全拼</option>
                <option value="首拼">首拼</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">指标名称</label>
        <div class="layui-input-block">
            <input type="text" name="IndexName" id="IndexName" placeholder="指标名称" autocomplete="off" class="layui-input" lay-verify="required" maxlength="30">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">指标类型</label>
        <div class="layui-input-block">
            <select id="IndexDataType" name="IndexDataType" lay-filter="IndexDataType" lay-verify='required'>
                <option value="">==请选择==</option>
                @foreach (IndexDtaeTypeEntity item in ((IEnumerable<IndexDtaeTypeEntity>)ViewBag.DtaeTypeList))
                {
    <option value="@item.TypeId">@item.TypeName</option>}
            </select>
            <tip>新建后不能再修改</tip>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">备注</label>
        <div class="layui-input-block">
            <input type="text" name="IndexExplain" placeholder="备注" autocomplete="off" class="layui-input" maxlength="100">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label"> </label>
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="add">新建指标</button>&nbsp;
            <input type="checkbox" id="continue" title="连续新建">
        </div>
    </div>
</form>
<script>
    layui.use(["form", "exLayer", "exUtils", "laydate"], function () {
        let form = layui.form;
        let layer = layui.layer;
        let exLayer = layui.exLayer;
        let exUtils = layui.exUtils;
        let laydate = layui.laydate;

        let $ = layui.$;

        form.render(); //初始化表单，要加上，不然刷新部分组件可能会不加载

        form.verify({
            id: function (value, item) {
                if (!new RegExp("^[a-zA-Z]+[a-zA-Z0-9]+$").test(value)) {
                    return '编码由英文字符和数字组成，并以字母开头，不得包含中文、空格及其他字符';
                }
            }
        });

        form.on("submit(add)", function (data) {
            exUtils.ajax("/SysTable/IndexBasic/Add", "post", data.field, true).done(function (response) {
                exLayer.greenTickMsg(response.message, function () {
                    if ($("#continue").is(':checked')) {
                        $("#IndexId").val("");
                        $("#IndexName").val("");
                    }
                    else {
                        if ($("#frm").val() == 'list') {
                            $(window.parent.document).find('#search').click();//刷新
                        }
                        else {
                            parent.layer.close(parent.layer.getFrameIndex(window.name));
                        }
                    }
                });
            }).fail(function (error) {
                console.log(error);
            });
            return false;
        });

        //自动生成编码
        form.on('select(selectfilter)', function (data) {
            var name = $("#IndexName").val().trim();
            var type = data.value;
            if (name == "") {
                exLayer.yellowSighMsg("请输入名称");
                return false;
            }
            exUtils.ajax("/SysBasic/AutoCode/CreatCode", "post", { name: name, type: type }, true).done(function (response) {
                $("#IndexId").val(response.extra);
            }).fail(function (error) {
                console.log(error);
            });
            return false;
        });
    });
</script>